<?php namespace App\Controllers;

use CodeIgniter\Controller;
use \App\Models\BlogModel;
use \App\Andy\Utility;
use \App\Andy\ImgPreCheck;
use CodeIgniter\I18n\Time;
use \App\Andy\BlogManager;



class Blog  extends BaseController
{
		
     protected $blogId;
     protected $myDate;
     protected $myTime;
     protected $blogImages="blogImages";

				 public function __construct() 
				    {
						parent::__construct();
						$this->myTime = parent::getTime();
						$this->myDate= date("d/m/Y",$this->myTime); 	
					
			       }

                   public function altMethod()
                   {
					
					$BlogHandle = new BlogManager(model(BlogModel::class));
					
					$result= $BlogHandle->list();
					var_dump($result);
					
					echo "place holder for trying dependency injection";   
					   
				   }
                   

					public function doCount()
					{
					$handle = new BlogModel();	
					$mycount= $handle->count();
					}		 

					public function doPaginate()
					{
					
					 $handle = new BlogModel();	
					$newresult = 	 $handle->getDesending();	
							
					//$result= $handle->getAll();
					 $data = [
						 'title'=>'paginate',
					  //'blogs' => $handle->paginate(5),
					    'blogs'=>$newresult->paginate(5),
						'pager' => $handle->pager,
						'date'=>$this->myDate
					];
					
					
					
					echo view('listBlogsPaginate',$data);
					
						
						
					}		

		
					public function showArticle($theSlug)
					
					{
					 
				 
					
					
					
					$handle = new BlogModel();
					$result= $handle->getArticle($theSlug);
						
					$data =[
					'title'=>$theSlug,
					'blog'=>$result,
					'date'=>$this->myDate
					];
							
						
					echo view('blogArticle',$data);
					
						
					
					}
					
		
					public function listBlogs()
					
					
					{
					
					$handle = new BlogModel();
					$result= $handle->getAll();
					$data =[
					'title'=>'andything',
					'blogs'=>$result,
					'date'=>$this->myDate
					];
							
						
					echo view('listBlogs',$data);
					
					
						
					
					}
		
		
		
		
		
		
		
					public function blogForm()
							{
					// displays form to submit newblog
					
				
					
					$data =[
					'title'=>'new blog',
					'info'=>'&nbsp;',
					'date'=>$this->myDate
					];
							
						
					echo view('form4',$data);
					
					
					
					}
		
		
		
		


            public function newBlogArticle()
	    {
	     
	 
				
			  
				//accepts post data from form4 and processes form submission of data for new blog 
				//what im going to do is filter out bad chars 
				$title = $this->request->getVar('title'); 
			   	$date=  $this->request->getVar('date');
			    
			    
			    $article = $this->request->getVar('article');
		   	    
		   	    
		   	    //now to remove ALL bad chars, can always do an edit 
		   	   
		   	    $utilityHandle= new Utility();
		   	   
		   	    $cleanTitle=$utilityHandle->badCharsRemoveAll($title);
		   	     $cleanDate= $utilityHandle->badCharsRemoveAll($date);
		   	   
		   	     $cleanArticle1 = $utilityHandle->removeScript($article);
		   	     $cleanArticle2= $utilityHandle->convertQuotes($cleanArticle1);
		   	     $cleanArticle = $utilityHandle->newRegex($cleanArticle2);
		   	   
		    	   	   
		   	   
		   	   
		   	    $file = $this->request->getFile('userfile');
		    	$name= $file->getName(); 
		        $tempfile = $file->getTempName();
		    	$slug= url_title($cleanTitle);
	 
	 
	 
	 
	    
			 //nowsome pre checks 
			  $handle = new ImgPreCheck();
			  //ImgPreCheck is one of my classes
			   
	  
	 
	  
				  $logic = $handle->allReadyExists($this->blogImages,$name);
				  if($logic == "true")
				 
				 {
					$data =[
					'title'=>'andything',
					'info'=>'image with that name already exists',
					'date'=>$this->myDate
					];
							
						
					echo view('form4',$data);
					
						exit();
				 }
				 
	
				$logic2 = $handle->extCheck($name);
					
				if($logic2 == 0)
				
				{
				
					$data =[
					'title'=>'andything',
					'info'=>'that is not an image file',
					'date'=>$this->myDate
					];							
						
					echo view('form4',$data);
					
				 exit();
				
				}
	
	
	

				 $size= $handle->getFileSize($tempfile);
				
				if($size > 500000)
				
				{
				$data =[
					'title'=>'andything',
					'info'=>'thats too large an image file',
					'date'=>$this->myDate
					];
							
						
					echo view('form4',$data);
					
				 exit();
				
				}
	
	
		
	
	    
	  try{
	  
	 $file->move('blogImages',$name);
	 $handle = new BlogModel();
      $blogLogic=	$handle->insertBlog($cleanTitle,$cleanArticle,$name,$slug,$cleanDate);





	 
 }
	 
	catch ( \Exception  $e)
			 {
			 			 
			 $data = [
						'title'  => 'info',
					  
				   'info'=>'somwething went wrong with new blog' ,
				   'infoException'=>$e->getMessage(),
				   'date'=>$this->myDate,
					 ];
			
				echo view('info2', $data);
				 die();
			 //above try catch works  and outputs rendered issue to view 
			    }
	 
	 
	 
	 if ($blogLogic == true)
	 
	  
	 {
	 
	 
	 
	 
	  $data =[
		'title'=>'blog submission',
		'info'=>'looks like new blog was created ',
		'date'=>$date
		];
				
  	        
		echo view('info',$data);
		
	 exit();
	
	}
	   
	   
	   
	   
	    }//end new blog 
	    
	    public function delBlogForm()
	      {
	     
	      $myTime = new Time('now');
		 $date=	$myTime->toLocalizedString('MMM d, yyyy');   
	 
	     
	     
	      helper('form');
	     helper('html');
	    
	    $handle = New BlogModel();
	    $result= $handle->getAll();
	    
	    
	    
	    $data =[
		'title'=>'andything',
		'blogList'=>$result,
		'info'=> '',
		'date'=>$this->myDate
		
		
		];
				
  	        
		echo view('removeBlogForm',$data);
		
	    
	    //end of newblog below
	    
	    }
	    
	    public function delBlog()
	    
	    {
			 $myTime = new Time('now');
		 $date=	$myTime->toLocalizedString('MMM d, yyyy');   
	 
			
	  helper('uri');
		helper('html');
		helper('form');
	helper('security');  
	    $this->blogId  = $this->request->getVar('BlogId'); 
	    $handle= new BlogModel();
	    $result=  $handle->getOneToDel($this->blogId);
	    
	     if($result==null)
	   {
		
		 
		 $handle= new BlogModel(); 
		$result= $handle->getAll();  
		  
		  
		 $data = [
                'title'  => 'del ',
                'blogList'=>$result,
                'info'=>'no such item  ',
                'date'=>$this->myDate
		];
		
		
        echo view('removeBlogForm',$data);
       
		 exit();  
		   
	   }
	   
	    
	    
	    
	    
	    
	    $image=  $result['image'];
	    //first get rid of image 
	  
	  try
	  {
	  //  chmod(ROOTPATH.'public/blogImages/'.$image,0777);
	    
	    
	    unlink(ROOTPATH.'public/blogImages/'.$image);
	    //now need to remove entry from databse
	  $logic=  $handle->deleteOne($this->blogId);
	  
	  }
	  
	  catch ( \Exception  $e)
			 {
			 $data = [
						'title'  => 'info',
					  
				   'info'=>'something  went wrong with deleting blog',
				   'infoException'=>$e->getMessage(),
				    'date'=>$date
					 ];

				
				echo view('info2', $data);
				
				  
		   
			 die();
			 //above try catch works 
		   }
	  
	  
	  
	  
	   if ($logic == True)
	   {
		  $data =[
		'title'=>'del blog',
		'info'=>'lookes like blog was deleted ',
		'date'=>$this->myDate
		
		];
				
  	       
		echo view('admin',$data);
		
		   
	 } 	   
	  
	    }
	    
	    
	    public function editBlogForm()
	      {
			//will try utility class to strip bad chars   
			
			//here another approach is to first convert all quotes to html entitiy 
			//then in a second round remove bad chars
	    $handle = new BlogModel();
		$result= $handle->getAll();
		$data =[
		'title'=>'andything',
		'blogs'=>$result,
		'date'=>$this->myDate
		];
				
  	       
		echo view('editBlogForm',$data);
		
	    
	    	    
	    }
	    
	    
	    
	    
	    public function editBlog($slug)
	    
	    {
	   
	    
	    
	    $handle= new BlogModel();
	    $result= $handle->getArticle($slug);
	    
	   
	   
	   $data =[
		'title'=>'andything',
		'blog'=>$result,
		'date'=>$this->myDate
		];
				
  	       
		echo view('doEditBlog',$data);
		
	   
	   
	    }
	    
	    
	    
	    public function processEditBlog()
	    
	    {
			
			
			
	  
	    
	     $title = $this->request->getVar('title'); 
	   $article = $this->request->getVar('article');
	   $blogId= $this->request->getVar('Id');
	    $slug= url_title($title);
	   
	   $utilityHandle= new Utility();
	   
	   $cleanTitle=$utilityHandle->badCharsRemoveAll($title);
	   
	  $cleanArticle1 = $utilityHandle->removeScript($article);
	$cleanArticle2= $utilityHandle->convertQuotes($cleanArticle1);
	$cleanArticle3 = $utilityHandle->newRegex($cleanArticle2);
	  
	   
	   
	   
	   
	   try
	   {
	   $handle= new BlogModel();
	   
	$result=   $handle->amendBlog($blogId,$cleanTitle,$cleanArticle3,$slug);
   }
   
   catch ( \Exception  $e)
			 {
			 $data = [
						'title'  => 'info',
					  
				   'info'=>'something went wrong with editing blog ',
				   'infoException'=>$e->getMessage(),
				   'date'=>$this->myDate
					 ];

				
				echo view('info2', $data);
				
				
				  
		   
			 die();
			 //above try catch works 
		   }
   
   
   
   
	   
	     if ($result == 1) 
	     {
	     
	     
	     $data =[
		'title'=>'blog result',
		'info'=>'lookes like edit of blog was successful',
		'date'=>$this->myDate
		];
				
  	       
		echo view('admin',$data);
		
	     
	     
	     }
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    
	    }
	    
	    
	    
	   
	    
	    
	    
	    

}
